package com.jz.banner.screen.utils

import android.annotation.SuppressLint
import com.lzy.okgo.model.HttpHeaders
import com.lzy.okgo.model.HttpParams
import com.jz.banner.screen.bean.DevicesInfoBean
import java.util.*

/**
 * 创建者     Noel
 * 创建时间   2018/1/8 9:51
 * 描述	      url请求地址。
 */

object Params {
    const val mar_name: String = "mar_name"
    const val stall_name: String = "stall_name"
    const val shop_name: String = "shop_name"
    const val contact_name: String = "contact_name"
    const val contact_mobile: String = "contact_mobile"
    const val shop_cat_name: String = "shop_cat_name"

    const val title_copyright: String = "title_copyright"
    const val merchant_screen_type: String = "merchant_screen_type"
    const val device_number: String = "device_number"
    const val password: String = "password"
    const val pwd: String = "pwd"
    const val phone: String = "phone"
    const val mobile: String = "mobile"
    const val new_version: String = "new_version"
    const val is_auto_login: String = "is_auto_login"

    const val time_polling: String = "time_polling"
    const val user_id: String = "user_id"
    const val com_id: String = "com_id"
    const val date: String = "date"
    const val screen_type: String = "screen_type"

    const val status: String = "status"
    const val code: String = "code"
    const val id: String = "id"
    const val BaseUrl: String = "BaseUrl"
    const val tel: String = "tel"
    const val device_type: String = "device_type"
    const val device_firm: String = "device_firm"
    const val device_system_version: String = "device_system_version"
    const val device_name: String = "device_name"
    const val device_board_name: String = "device_board_name"
    const val device_firm_name: String = "device_firm_name"
    const val version: String = "loginType"
    const val device_info: String = "scene"
    const val url: String = "url"
    const val sign: String = "sign"
    const val token: String = "token"

    const val app_name: String = "app_name"
    const val type: String = "type"
    const val image: String = "image"
    const val scale: String = "scale"
    const val level: String = "level"
    const val md5_update: String = "md5_update"
    const val name: String = "name"
    const val mer_id: String = "mer_id"
    const val key: String = "key"

    const val action: String = "action"
    const val BaseImgUrl: String = "BaseImgUrl"
    const val info: String = "info"
    const val encrypt_member_code: String = "action"
    const val uuid: String = "uuid"
    const val client_type: String = "client_type"
    const val timestamp: String = "timestamp"
    const val future_json: String = "future_json"
    const val app_unit_id: String = "app_unit_id"
    const val device_info_json: String = "device_info_json"


    /*新参数*/
    fun getParams(map: SortedMap<String, Any?>, isSign: Boolean = true): HttpParams {

        /*map.entries.forEach {
            CommUtils.println("sign:", "k:${it.key},v:$${it.value}")
        }
        for ((k, v) in map) {
            CommUtils.println("sign:", "k:$k,v:$v")
        }*/
//        map[token] = SPUtils.getToken()
//        map[uuid] = CommUtils.getAppUUid()
        map[app_unit_id] = CommUtils.getAppUUid()
        map[device_info_json] = getDeviceInfoJson()
        map[future_json] = SPUtils.getString(future_json)
        map[screen_type] = 0 //screen_type，0是普通，1是蓝牙屏幕。
        map[client_type] = 21
        return HttpParams().apply {
            put(sign, getSign(map))
        }
    }

    @SuppressLint("MissingPermission")
    fun getDeviceInfoJson(): String {
        return CommUtils.gson.toJson(DevicesInfoBean().apply {
             device_firm =EquipmentUtil.getDeviceBrand() //手机厂商
             device_type= EquipmentUtil.getSystemModel()   //手机型号
             device_system_version= EquipmentUtil.getSystemVersion() //Android系统版本号
             device_name= EquipmentUtil.getSystemDevice() //手机设备名
             device_board_name= EquipmentUtil.getDeviceBoard() //主板名
             device_firm_name = EquipmentUtil.getDeviceManufacturer() //手机厂商名
             version = CommUtils.getAppVersionName()//当前版本
        })
    }

    @SuppressLint("MissingPermission")
    fun getHeads(map: SortedMap<String, String?> = sortedMapOf()): HttpHeaders {
        val toJson = getDeviceInfoJson()
            return HttpHeaders().apply {
                put(future_json, SPUtils.getToken())
                put(com_id, SPUtils.getString(com_id))
                put(version, CommUtils.getAppVersionName())
                put(device_type, "android")
                put(device_info, toJson)
                map.forEach {
                    if (!it.key.isNullOrBlank() && !it.value.isNullOrBlank()) put(it.key, it.value)
                }
            }
    }

    /*新参数*/
    /* fun getPayParams(map: SortedMap<String, Any?>, isSign: Boolean = true): HttpParams {
         *//*map.entries.forEach {
            CommUtils.println("sign:", "k:${it.key},v:$${it.value}")
        }
        for ((k, v) in map) {
            CommUtils.println("sign:", "k:$k,v:$v")
        }*//*
        return HttpParams().apply {
            var paySign = getPaySign(map)
            if (isSign) {
                put(sign, paySign)
            }
        }
    }*/

}

/*会员卡支付签名*/
/*
fun HttpParams.getPaySign(map: SortedMap<String, Any?>): String {
    return CommUtils.encodeByMD5(StringBuilder().let {
        for ((key, value) in map) {
            CommUtils.println("PaySign:", "key：$key,value:$value")
            if (value != null && !value.toString().isBlank()) {
                it
                    .append(key.toString())
                    .append(value)
                this.put(key.toString(), value.toString())
            }
        }
        "app_secret".plus(Urls.CommunicationKey).plus(it.toString())
            .apply {
                CommUtils.println("PaySign:", "排序后：$this")
            }
    })
}
*/

/*本地签名*/
fun HttpParams.getSign(map: SortedMap<String, Any?>): String {
    return CommUtils.encodeByMD5(StringBuilder().let {
        for ((key, value) in map) {
            if (value != null && !value.toString().isBlank()) {
                it.append(key.toString())
                    .append("=")
                    .append(value)
                    .append("&")
                this.put(key.toString(), value.toString())
            }
        }
        //密钥
        if (it.isNotEmpty()) it.deleteCharAt(it.lastIndex)
        it.append("niulebileti")
            .toString()
            .apply {
                CommUtils.println("sign", "排序后：$this")
            }
    })
}
